package com.learn.dubbo.client;

import com.learn.dubbo.services.UserService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.ReferenceConfig;
import org.apache.dubbo.config.RegistryConfig;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

/**
 * @author wangxing
 * @version 2020/6/8 11:31 Administrator
 */
public class DubboClient {
    public static void main(String[] args) throws IOException {
        DubboClient d = new DubboClient();
        BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
        while (true) {
            if ("quit".equals(bufferedReader.readLine())) {
                break;
            }
            System.out.println(d.buildRemoteService("url").getUser(1));
        }
    }

    public UserService buildRemoteService(String remoteUrl) {
//        应用
        ApplicationConfig applicationConfig = new ApplicationConfig("client");
//        引用
        ReferenceConfig<UserService> referenceConfig = new ReferenceConfig<>();
        RegistryConfig registryConfig = new RegistryConfig("multicast://224.1.1.1:3333");
        referenceConfig.setApplication(applicationConfig);
//        referenceConfig.setUrl(remoteUrl);
        referenceConfig.setRegistry(registryConfig);
        //设置接口
        referenceConfig.setInterface(UserService.class);
        referenceConfig.setLoadbalance("roundrobin");//负载均衡,轮询
        UserService userService = referenceConfig.get();
        return userService;
    }
}
